package cn.e2win.biz.sms;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e2win.base.entity.Msg;
import cn.e2win.base.entity.MsgLogs;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.base.json.vo.Result;
import cn.e2win.biz.annotation.PageParam;
import cn.e2win.biz.base.BaseController;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.comm.SmsSender;
import cn.e2win.biz.util.StringUtils;


@Controller
@Scope("request")
@RequestMapping("/msg")
public class MsgController extends BaseController {

	@Autowired
	private MsgService msgService;
	
	@Override
	public BaseService initService() {
		return msgService;
	}
	//发送短信到客户
	@RequestMapping("initMsg2Customer")
	public String initMsg2Customer(HttpServletRequest request, @PageParam Pager pager, ModelMap modelMap){
//		pager.setReturnAll(true);
		String numPerPage = request.getParameter("numPerPage");
		if(StringUtils.isEmpty(numPerPage)){
			pager.setNumPerPage(100);
		}else{
			pager.setNumPerPage(Integer.parseInt(numPerPage));
		}
		msgService.searchCust4SendMsg(pager);
		
		modelMap.put("pager", pager);
		return "msg/sms2Customer";
	}
	//发送短信到指定号码
	@RequestMapping("initMsg2PhoneNo")
	public String initMsg2PhoneNo(HttpServletRequest request,  ModelMap modelMap){
		return "msg/sms2PhoneNo";
	}
	//批量发送短信
	@RequestMapping("sendBatchMsg")
	@ResponseBody
	public Result sendBatchMsg(HttpServletRequest request,  @ModelAttribute Msg msg, ModelMap modelMap){
		Result r = new Result();
		try {
			List<MsgLogs> logList = new ArrayList<MsgLogs>();
			String[] toNo = request.getParameterValues(ITEMS + "toNo");			
			String[] toCust = request.getParameterValues(ITEMS + "toCust");
			if(toNo!=null && toNo.length>0){
				for (int i = 0; i < toNo.length; i++) {
					MsgLogs msgLog = new MsgLogs();
					msgLog.setToNo(toNo[i]);
					if(toCust[i]!=null && !StringUtils.isEmpty(toCust[i]) && StringUtils.isNumeric(toCust[i])){
						msgLog.setCustId(Integer.parseInt(toCust[i]));
					}
					msgLog.setMsgType("SMS_BATCH");
					logList.add(msgLog);
				}
			}
			String phoneNos = request.getParameter("phoneNos");
			processCellNo(phoneNos, "\n", logList);
			
			//msgService.sendBatchMsg(msg, logList);
			r.setMessage("消息已开始批量发送至  "+ logList.size() + "  个号码!");
			r.setCallbackType(Result.CLOSE_CURRENT);
		}catch(Exception e){
			r.setStatusCode(Result.FAILED);
			r.setException(e);
		}
		return r;
	}
	
	private void processCellNo(String phoneNos, String regex, List<MsgLogs> list){
		String[] cellNos = phoneNos.split(regex);
		for(String cellNo : cellNos){
			cellNo = cellNo.trim();
			if(StringUtils.isCellNo(cellNo)){
				MsgLogs msgLog = new MsgLogs();
				msgLog.setToNo(cellNo);
				msgLog.setMsgType("SMS_BATCH");
				list.add(msgLog);
			}else if(cellNo.length()>11){
				processCellNo(cellNo, ",", list);
			}
		}
	}
	
	
	//发送短信到客户
	@RequestMapping("sendSingleMsg")
	@ResponseBody
	public Result sendSingleMsg(HttpServletRequest request,  @ModelAttribute MsgLogs msgLog, ModelMap modelMap){
		Result r = new Result();
		try {
			msgLog.setMsgType("SMS_TEST");
			msgLog = msgService.sendSingleMsg(msgLog);
			r.setMessage("消息已发送至" + msgLog.getToNo() + "计" + msgLog.getQty() +"条,请注意查收!");
			r.setCallbackType(Result.SUCCESS);
		}catch(Exception e){
			r.setStatusCode(Result.FAILED);
			r.setException(e);
		}
		return r;
	}
	
	
	
	//日志查询
	@RequestMapping("msgLogsPager")
	public String msgLogsPager(HttpServletRequest request, @PageParam Pager pager,
			ModelMap modelMap) {
		try {
			msgService.pagerMsgLogs(pager);
			
			modelMap.put("pager", pager);
			return "msg/msgLogsPager";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return "msg/common/fail";
		}
	}

}
